package com.LachesismStorm.lachesism.downloader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class FetchHtmlWithCookies {
    public static void main(String[] args) {
        String url = "https://m.ip138.com/iplookup.php?from=baidu&ip=113.132.131.64";
        String cookies = "BAIDU_SSP_lcr=https://baidu.com/; "
                + "Hm_lpvt_4513768e33b6903827b5a4ff6fef7639=1740852167; "
                + "Hm_lvt_4513768e33b6903827b5a4ff6fef7639=1740851270; "
                + "HMACCOUNT=4297E526EDF21B7E; "
                + "PHPSESSID=t3riq3kq09dh2kpcnq5ov1k0go; "
                + "tfstk=gQ0sgSxzhx3eV30WZddUOo08uazm5fTPCsNxZjQNMPUOk-Hrgohauled";

        try {
            URL obj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            connection.setRequestMethod("GET");

            // 设置请求头
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Cookie", cookies);

            // 读取响应
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine).append("\n");
                }
                in.close();

                // 输出HTML内容
                System.out.println("页面内容：\n" + response.toString());
            } else {
                System.out.println("请求失败，响应码：" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}